package com.webdesign.action;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.webdesign.entity.TbUser;
import com.webdesign.service.ITbUserService;
import com.webdesign.util.MD5;



@Controller("StuChangePwdAction")
@Scope("prototype")
public class StuChangePwdAction {
	private static final String REDIRECT = "redirect";
	private ITbUserService iTbUserService;
	
	/*学生修改密码功能
	 * password是学生原密码
	 * newpwd是学生要修改的新密码
	 * flag是判断是否修改成功的字符串变量，为success表示修改成功，为wrong表示修改失败
	 */
	private String password;
	private String newpwd;
	private String flag;
	
	
	//学生点击修改密码按钮
	public String changePwd(){
		return "success";
	}
	
	
	/*
	 * 学生修改密码
	 */
	public String stuRewrite(){
		MD5 md = new MD5();
		//将原密码、新密码进行MD5加密
		String passwordMD5 = md.MD5(password);
		String newpwdMD5 = md.MD5(newpwd);
		Map<String,Object> session = ActionContext.getContext().getSession();
		//userid是学生学号
		String userid =  (String)session.get("userid");
		TbUser tbUser = iTbUserService.stuRewrite(userid, passwordMD5, newpwdMD5);
		if(tbUser != null){
			flag = "success";
		}else{
			flag = "wrong";
		}
		return "success";
	}

	public ITbUserService getiTbUserService() {
		return iTbUserService;
	}

	@Resource(name="TbUserService")
	public void setiTbUserService(ITbUserService iTbUserService) {
		this.iTbUserService = iTbUserService;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getNewpwd() {
		return newpwd;
	}

	public void setNewpwd(String newpwd) {
		this.newpwd = newpwd;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}
	
	
	
}
